package com.farm.parameter;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.farm.core.ParameterService;
import com.farm.parameter.service.DictionaryEntityManagerInter;
import com.farm.parameter.service.ParameterManagerInter;
import com.farm.parameter.service.impl.ConstantVarService;
import com.farm.parameter.service.impl.PropertiesFileService;
import com.farm.web.constant.FarmConstant;
import com.farm.web.spring.BeanFactory;

/**
 * 框架系统参数服务
 * 
 * @author Administrator
 * 
 */
public class FarmParameterService implements ParameterService {
	private final static ParameterManagerInter parameters = (ParameterManagerInter) BeanFactory
			.getBean("ALONE_Parameter_DAO_PROXY");
	private final static DictionaryEntityManagerInter dictionaryentitys = (DictionaryEntityManagerInter) BeanFactory
			.getBean("ALONE_DictionaryEntity_DAO_PROXY");

	public void setPropertiesFiles(List<String> propertiesFiles) {
		for (String name : propertiesFiles) {
			if (PropertiesFileService.registConstant(name)) {
				System.out
						.println("注册配置文件:"
								+ name
								+ ".properties(com.farm.parameter.FarmParameterService)");
			}
		}
	}

	static {
		// 注册常量
		ConstantVarService.registConstant(
				"farm.constant.session.key.logintime.",
				FarmConstant.SESSION_LOGINTIME);
		ConstantVarService.registConstant(
				"farm.constant.session.key.current.org",
				FarmConstant.SESSION_ORG);
		ConstantVarService.registConstant(
				"farm.constant.session.key.current.roles",
				FarmConstant.SESSION_ROLES);
		ConstantVarService.registConstant(
				"farm.constant.session.key.current.actions",
				FarmConstant.SESSION_USERACTION);
		ConstantVarService.registConstant(
				"farm.constant.session.key.current.menu",
				FarmConstant.SESSION_USERMENU);
		ConstantVarService.registConstant(
				"farm.constant.session.key.current.user",
				FarmConstant.SESSION_USEROBJ);
		ConstantVarService.registConstant(
				"farm.constant.session.key.current.userphoto",
				FarmConstant.SESSION_USERPHOTO);
		ConstantVarService.registConstant("farm.constant.app.treecodelen",
				String.valueOf(FarmConstant.MENU_TREECODE_UNIT_LENGTH));
		// 注册配置文件
		PropertiesFileService.registConstant("config");

	}

	@Override
	public Map<String, String> getDictionary(String key) {
		return dictionaryentitys.getDictionary(key);
	}

	@Override
	public List<Entry<String, String>> getDictionaryList(String key) {
		return dictionaryentitys.getDictionaryList(key);
	}

	@Override
	public String getParameter(String key) {
		key = key.trim();
		// 先找用户参数和系统参数
		String value = parameters.getValue(key);
		if (value != null) {
			return value;
		}
		// 再找properties文件参数
		value = PropertiesFileService.getValue(key);
		if (value != null) {
			return value;
		}
		// 找常量
		value = ConstantVarService.getValue(key);
		if (value != null) {
			return value;
		}
		throw new RuntimeException("无法获得参数:" + key);
	}

	@Override
	public String getParameter(String key, String userId) {
		String value = parameters.getValue(key, userId);
		if (value != null) {
			return value;
		}
		// 再找properties文件参数
		value = PropertiesFileService.getValue(key);
		if (value != null) {
			return value;
		}
		// 找常量
		value = ConstantVarService.getValue(key);
		if (value != null) {
			return value;
		}
		throw new RuntimeException("无法获得参数:" + key);
	}
}
